package com.jfs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jfs.entity.DeviceChannel;
import com.jfs.entity.dto.DeviceChannelListDto;
import com.jfs.entity.vo.DeviceChannelListVo;
import com.jfs.entity.vo.DeviceChannelUpdateByNameVo;
import com.jfs.mapper.DeviceChannelMaaper;
import com.jfs.result.Result;
import com.jfs.service.DeviceChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description 通道接口实现层
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/8/3
 */
@Service
@Slf4j
public class DeviceChannelServiceImpl extends ServiceImpl<DeviceChannelMaaper, DeviceChannel> implements DeviceChannelService {

    private final static List<String> models = new ArrayList<String>() {
        {
            add("AlarmIn");
        }
    };

    /**
     * 条件查询通道列表
     *
     * @param deviceChannelListVo
     * @return
     */
    @Override
    public IPage<DeviceChannelListDto> list(DeviceChannelListVo deviceChannelListVo) {
        List<DeviceChannelListDto> deviceChannelListDtos = new ArrayList<>();
        Page<DeviceChannelListDto> deviceChannelListDtoPage = new Page<>();
        Page<DeviceChannel> deviceChannelPage = this.page(new Page<>(deviceChannelListVo.getPageNum(), deviceChannelListVo.getPageSize()),
                Wrappers.<DeviceChannel>lambdaQuery()
                        //此处主要是为了排除告警通道
                        .notIn(DeviceChannel::getModel,models)
                        //根据设备id
                        .eq(StrUtil.isNotEmpty(deviceChannelListVo.getDeviceId()), DeviceChannel::getDeviceId, deviceChannelListVo.getDeviceId())
                        //根据关键字搜索
                        .and(StrUtil.isNotEmpty(deviceChannelListVo.getSearchKey()), wrap ->
                                //设备名
                                wrap.like(DeviceChannel::getName, deviceChannelListVo.getSearchKey())
                                        .or()
                                        //通道编号
                                        .like(DeviceChannel::getChannelId, deviceChannelListVo.getSearchKey())
                                        .or()
                                        //制造商
                                        .like(DeviceChannel::getManufacturer, deviceChannelListVo.getSearchKey())
                        )
                        //根据状态搜索
                        .eq(Objects.nonNull(deviceChannelListVo.getOnline()), DeviceChannel::getStatus, deviceChannelListVo.getOnline())
        );
        //转换为dto输出
        List<DeviceChannel> records = deviceChannelPage.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            for (DeviceChannel deviceChannel : records
            ) {
                DeviceChannelListDto deviceChannelListDto = new DeviceChannelListDto();
                BeanUtils.copyProperties(deviceChannel, deviceChannelListDto);
                deviceChannelListDtos.add(deviceChannelListDto);
            }
            //设置分页参数
            BeanUtils.copyProperties(deviceChannelPage, deviceChannelListDtoPage);
            deviceChannelListDtoPage.setRecords(deviceChannelListDtos);
        }
        return deviceChannelListDtoPage;
    }

    /**
     * 修改通道名称
     *
     * @param deviceChannelUpdateByNameVo
     * @return
     */
    @Override
    @Transactional
    public Result<Object> updateByName(DeviceChannelUpdateByNameVo deviceChannelUpdateByNameVo) {
        boolean update = this.update(Wrappers.<DeviceChannel>lambdaUpdate()
                .set(DeviceChannel::getName, deviceChannelUpdateByNameVo.getName())
                .eq(DeviceChannel::getChannelId, deviceChannelUpdateByNameVo.getChannelId()));
        if (update) {
            return Result.success("修改通道名称成功", null);
        }
        return Result.failed("修改通道名称失败");
    }
}
